<template>
  <view class="my-board-mask" v-if="popupIdx" @click="popupIdx = ''"></view>
  <view :class="`my-board ${props.preview && 'preview'}`">
    <view class="row" v-for="(row, x) in props.matrix">
      <view
        :class="`columns ${cell === 1 && 'active'} ${cell === -1 && 'error'}`"
        v-for="(cell, y) in row"
        @click="cellClick(cell, x, y)"
      >
        <view
          :class="['popup', `${x}${y}` === popupIdx && 'show']"
          @click.stop="popupClick(x, y)"
        >
          删除
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref } from "vue";

const props = defineProps<{
  preview?: boolean;
  matrix: (-1 | 0 | 1)[][];
}>();
const emit = defineEmits<{
  (e: "click", val: 0 | 1, x: number, y: number): void;
}>();

const popupIdx = ref("");
const cellClick = (cell: number, x: number, y: number) => {
  if (cell === 0) {
    emit("click", 0, x, y);
  } else {
    popupIdx.value = `${x}${y}`;
  }
};
const popupClick = (x: number, y: number) => {
  emit("click", 1, x, y);
  popupIdx.value = "";
};
</script>

<style scoped lang="scss">
.my-board-mask {
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  z-index: 99;
}

.my-board {
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 100%;
  aspect-ratio: 1 / 1;
  box-sizing: border-box;
  padding: 8px;
  border-radius: 8px;
  background: linear-gradient(315deg, #dfa398 0%, #e89d90 99%);
  box-shadow: 0px 8px 15px 0px rgba(255, 146, 103, 0.3);
  &.preview {
    pointer-events: none;
  }

  .row {
    flex: 1;
    display: flex;
    gap: 3px;

    &:nth-child(2n) .columns {
      background-color: #f5bcb3;
      &:nth-child(2n) {
        background-color: #ffdfd8;
      }
    }
    .columns {
      flex: 1;
      position: relative;
      border-radius: 2px;
      background-color: #ffdfd8;
      &:nth-child(2n) {
        background-color: #f5bcb3;
      }
      &.active,
      &.error {
        background-image: url(/static/img/qi_zi.png);
        background-size: 90% 90%;
        background-position: center;
        background-repeat: no-repeat;
      }
      &.error {
        outline: 1px solid #ff1600;
        background-color: rgba(255, 22, 0, 0.3) !important;
      }

      .popup {
        display: none;
        align-items: center;
        justify-content: center;
        position: absolute;
        left: 50%;
        top: -4px;
        z-index: 100;
        transform: translate(-50%, -100%);
        width: 50px;
        height: 23px;
        border-radius: 27px;
        background: rgba(255, 255, 255, 0.8);
        font-size: 13px;
        color: rgba(0, 0, 0, 0.6);
        &.show {
          display: flex;
        }
      }
    }
  }
}
</style>
